package yqt.com.cnki.controller;


import haha.life.common.CutPageBean;
import haha.life.normal.CommonConvertUtils;
import haha.life.normal.FileUtil;
import haha.life.normal.FileuploadUtils;
import io.swagger.annotations.ApiOperation;
import jzqb.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yqt.com.cnki.bo.KYDABo;
import yqt.com.cnki.bo.QKKMBo;
import yqt.com.cnki.bo.QKYDBo;
import yqt.com.cnki.po.*;
import yqt.com.cnki.service.IQKYDService;
import yqt.com.cnki.service.IWWSJService;
import yqt.com.cnki.service.impl.WWSJServiceImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description: 期刊阅读
 *
 * @author BaiGe
 * @date: 2021/11/12 14:25
 * @Version 1.0
 */
@RestController
@RequestMapping("/qkyd")
public class QKYDController {

    @Autowired
    private IQKYDService qkydService;
    @Autowired
    private IWWSJService wwsjService;


    @ApiOperation(value = "期刊_数据库dbCode获取")
    @GetMapping("/getDBCodeFromFile")
    public Result getDBCodeFromFile() {
        List<Map<String, String>> mapArr = new ArrayList<>();
        String xmlStr = FileUtil.readJsonFile("yqtConfig.xml");
        Map<String, String> requestMap = CommonConvertUtils.getXMLStringValue(xmlStr);
        // 获取指定标签值
        String s = requestMap.get("中文期刊数据库表");
        Map<String, String> map_chinese = new HashMap<>();
        map_chinese.put("name", "中文期刊");
        map_chinese.put("tableCode", s);

        String s2 = requestMap.get("方正外文期刊数据库表");
        Map<String, String> map_english = new HashMap<>();
        map_english.put("name", "方正外文");
        map_english.put("tableCode", s2);
        mapArr.add(map_chinese);
        mapArr.add(map_english);

        return Result.succ(mapArr);
    }


    @ApiOperation(value = "中文期刊获取年份")
    @GetMapping("/getYear_Chinese")
    public Result getYear_Chinese() {
        List<KeyValuePo> keyValuePos = qkydService.getYear_Chinese();
        return Result.succ(keyValuePos);
    }

    @ApiOperation(value = "外文期刊获取分类")
    @GetMapping("/getSort_English")
    public Result getSort_English() {
        List<KeyValuePo> keyValuePos = qkydService.getSort_English();
        return Result.succ(keyValuePos);
    }


    @ApiOperation(value = "期刊阅读_复合检索")
    @PostMapping("/complexSearchQKYD")
    public CutPageBean<QKYDPo> complexSearchQKYD(@RequestBody QKYDBo qkydBo) {
        CutPageBean<QKYDPo> cutPageBean = qkydService.complexSearchQKYD(qkydBo);
        return cutPageBean;
    }

    @ApiOperation(value = "外文原刊_复合检索")
    @PostMapping("/complexSearchWWSJ")
    public CutPageBean<WWSJPo> complexSearchWWSJ(@RequestBody KYDABo qkydBo) {
        CutPageBean<WWSJPo> cutPageBean = wwsjService.complexSearchWWSJ_List(qkydBo);
        return cutPageBean;
    }

    @ApiOperation(value = "方正外文-->根据SYSID获取METAID")
    @PostMapping("/getFZWWDMETAID")
    public Result complexSearchWWSJ(@RequestBody QKYDPo qkydPo) {
//       @GetMapping("/getFZWWDMETAID")
//    public Result complexSearchWWSJ(@RequestParam("sysId") String sysId,@RequestParam("tableCode") String tableCode) {
        QKYDPo qkydPoById = qkydService.getQKYDPoById(qkydPo);
        String drid = qkydPoById.getDrid();
        String metaId = wwsjService.getMetaIdByDrid(drid);
        return Result.succ(metaId);
    }


    @ApiOperation(value = "期刊刊名")
    @GetMapping("/getQKKM")
    public List<QKKMPo> getQKKM() {
        List<QKKMPo> ret = wwsjService.getQKKM();
        return ret;
    }

    @ApiOperation(value = "期刊刊名")
    @PostMapping("/getQKKMpageList")
    public  CutPageBean<QKKMPo>getQKKMpageList(@RequestBody QKKMBo qkkmBo) {
        CutPageBean<QKKMPo> cutPageBean = wwsjService.getQKKMpageList(qkkmBo);
        return cutPageBean;
    }

    @ApiOperation(value = "外文期刊获取年份")
    @GetMapping("/getYearForeign")
    public Result getYearForeign(String km) {
        List<KeyValuePo> keyValuePos = wwsjService.getYearForeign(km);
        return Result.succ(keyValuePos);
    }


    @ApiOperation(value = "通用_文件在线下载")
    @PostMapping("/common_onlineDownload")
    public Result common_onlineDownload(@RequestBody QKYDPo qkydPo) {
        QKYDPo qkydPoById = qkydService.getQKYDPoById(qkydPo);
        if (qkydPoById == null) {
            return Result.fail("文件不存在");
        }

        //方正外文库专属分支
        if ("YQTFZWW_METADATA".equals(qkydPoById.getTableCode())) {
            String drid = qkydPoById.getDrid();
            FZFile fzf = wwsjService.getFZFile(drid);
            if (fzf == null || " ".equals(fzf.getFileName())) {
                return Result.fail("文件不存在");
            }
            String localPath = "D:\\ApaDownLoadDlib";
            String fileId = fzf.getFileId();
            FZWUFILE fzwufile = wwsjService.getFZWUFILE(fileId);
            if (fzwufile == null || " ".equals(fzwufile.getUrlFileName())) {
                return Result.fail("文件不存在");
            }

            String overPath = localPath + "\\" + fzwufile.getUrlFileName();
            byte[] imageBinary = FileuploadUtils.getImageBinary(overPath);
            if (imageBinary == null || imageBinary.length == 0) {
                return Result.fail("文件不存在");
            }
            //增加下載量
            qkydService.addDownloadRate(qkydPo);
            return Result.succ(imageBinary);
        }

        if ("".equals(qkydPoById.getDigitFileName()) || qkydPoById.getDigitFileName() == null) {
            return Result.fail("文件不存在");
        }


        //查询文件存放的通用地址:前半截
        String fileBasePath = qkydService.getQKYDFileBasePath(qkydPo.getTableCode());
        //计算库名  YQTZWQK_METADATA->取前半截  YQTZWQK
        String dbCode = qkydPo.getTableCode().split("_")[0];
        //完整路径:前半截+计算出的后半截
        String overPath = FileUtil.calculateAbstractPath(qkydPoById.getDigitFileName(), fileBasePath + "\\" + dbCode + "\\Pages");
        //D:\YQTDataBase\TPI\Pages\517\天然气勘探开发动态2021年第02期-含水油气藏.pdf
       /* FileuploadUtils.download(overPath, response);
        return null;*/
        byte[] imageBinary = FileuploadUtils.getImageBinary(overPath);
        if (imageBinary == null || imageBinary.length == 0) {
            return Result.fail("文件不存在");
        }
        Boolean addDownloadRate = qkydService.addDownloadRate(qkydPo);
        return Result.succ(imageBinary);
    }

    @ApiOperation(value = "通用_文件在线预览")
    @PostMapping("/common_onlineView")
    public Result common_view(@RequestBody QKYDPo qkydPo) {
        QKYDPo bo = qkydService.getQKYDPoById(qkydPo);
        if (bo == null) {
            return Result.fail("文件不存在");
        }

        //方正外文库专属分支
        if ("YQTFZWW_METADATA".equals(bo.getTableCode())) {
            String drid = bo.getDrid();
            FZFile fzf = wwsjService.getFZFile(drid);
            if (fzf == null || " ".equals(fzf.getFileName())) {
                return Result.fail("文件不存在");
            } else {//方正文件不支持在线预览
                return Result.fail("不支持的文件类型，请下载后观看");
            }
        }

        if ("".equals(bo.getDigitFileName()) || bo.getDigitFileName() == null) {
            return Result.fail("文件不存在");
        }
        if (!bo.getDigitFileName().contains(".pdf")) {
            return Result.fail("不支持的文件类型，请下载后观看");
        }
        String fileBasePath = qkydService.getQKYDFileBasePath(qkydPo.getTableCode());
        //计算库名  YQTZWQK_METADATA->取前半截  YQTZWQK
        String dbCode = qkydPo.getTableCode().split("_")[0];
        //完整路径
        String overPath = FileUtil.calculateAbstractPath(bo.getDigitFileName(), fileBasePath + "\\" + dbCode + "\\Pages");
        byte[] imageBinary = FileuploadUtils.getImageBinary(overPath);
        if (imageBinary == null || imageBinary.length == 0) {
            return Result.fail("文件不存在");
        }

        Boolean addBrowseRate = qkydService.addBrowseRate(qkydPo);
        return Result.succ(imageBinary);
    }


}
